from django.http import JsonResponse
from django.shortcuts import render
from .models import *
import json

# Create your views here.


def userLogin(request):
    if request.method == 'POST':
        postBody = request.body
        json_result = json.loads(postBody)
        name = json_result.get("name")
        password = json_result.get("password")
        user = User.objects.get(name=name)
        if str(user.password) != str(password) or str(user.name) != str(name):
            return JsonResponse({'status': 400, 'message': "错了"})
        else:
            return JsonResponse({'status': 200, 'message': '登录成功', 'data': {'token': '123456'}})


def userRegist(request):
    if request.method == 'POST':
        postBody = request.body
        json_result = json.loads(postBody)
        name = json_result.get("name")
        password = json_result.get("password")
        print('register', name, password)
        User.objects.create(name=str(name), password=str(password))
        return JsonResponse({'status': 200, 'message': '注册成功'})
